package tw.twandroidproj.app.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import tw.twandroidproj.app.R;
import tw.twandroidproj.app.model.Bean;
import tw.twandroidproj.app.model.ErrorModel;
import tw.twandroidproj.app.model.Job;
import tw.twandroidproj.app.network.NetworkManager;
import tw.twandroidproj.app.network.OnResponseListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

//import tw.twandroidproj.app.Adapters.SearcherListViewAdapter;


public class SearcherActivity extends AppCompatActivity {
    private SearchView searchView;
    private Context context;
    private NetworkManager networkManager;
    private RecyclerView recyclerView;
    private NewsListAdapter listAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private ToggleButton toggleLeft,toggleRight,toggleMiddle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
         final myJobListener thisListener= new myJobListener();
        setSupportActionBar((Toolbar) findViewById(R.id.search_toolbar));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        initFilter();
//        int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
//        TextView textView = (TextView) searchView.findViewById(id);
//        textView.setTextColor(Color.BLACK);
//        textView.setHintTextColor(Color.GRAY);

        context = getBaseContext();
        networkManager=NetworkManager.getInstance(this);
        recyclerView=(RecyclerView)findViewById(R.id.search_job_list);
        mLayoutManager = new LinearLayoutManager(recyclerView.getContext());
        //设置布局管理器
        recyclerView.setLayoutManager(mLayoutManager);
//设置adapter
//设置Item增加、移除动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        searchView = (SearchView)findViewById(R.id.search_view);
        searchView.setIconified(false);
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Toast.makeText(context,"it submitted but ",Toast.LENGTH_LONG).show();
                networkManager.queryJob(query, 10,thisListener);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return true;
            }
        });
    }
    public void initFilter(){
        toggleLeft=(ToggleButton)findViewById(R.id.search_toggle_left);
        toggleMiddle=(ToggleButton)findViewById(R.id.search_toggle_middle);
        toggleRight=(ToggleButton)findViewById(R.id.search_toggle_right);
        final ArrayList<String> listSample = new ArrayList<String>(Arrays.asList("test", "test", "test"));
        toggleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View viewLeft = LayoutInflater.from(context).inflate(R.layout.activity_search_filter_view_left,null);
                ListView filterList =(ListView) viewLeft.findViewById(R.id.search_filter_left);
                filterList.setAdapter(new ArrayAdapter<String>(context,android.R.layout.simple_expandable_list_item_1,listSample));
                final PopupWindow popupWindow = new PopupWindow(viewLeft,
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                popupWindow.setTouchable(true);

                popupWindow.setTouchInterceptor(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {

                        Log.i("mengdd", "onTouch : ");

                        return false;
                        // 这里如果返回true的话，touch事件将被拦截
                        // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                    }
                });
                popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_input_line));
                popupWindow.showAsDropDown(view);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public void onBackPressed() {
        Intent intent=new Intent();
        intent.setClass(context,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
    public class NewsListAdapter extends RecyclerView.Adapter<NewsListAdapter.ViewHolder> {
        private Context mContext;
        private List<Job> mDataList;
        private LayoutInflater mLayoutInflater;

        public NewsListAdapter(Context Context, List<Job> DataList) {
            this.mContext = Context;
            this.mDataList = DataList;
            mLayoutInflater = LayoutInflater.from(mContext);
        }

        /**
         * 渲染具体的ViewHolder
         * @param viewGroup ViewHolder的容器
         * @param i 一个标志，我们根据该标志可以实现渲染不同类型的ViewHolder
         * @return
         */
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

                return new ViewHolder(mLayoutInflater.inflate(R.layout.searcher_cardview_item, viewGroup, false));

        }


        public void onBindViewHolder(ViewHolder jobItemHolder, int i) {
            Job entity = mDataList.get(i);

            if (null == entity){
                Toast.makeText(context,"There's no Job ",Toast.LENGTH_LONG).show();
                return;}
            jobItemHolder.position=i;
            jobItemHolder.JobName.setText(entity.getName());
            jobItemHolder.JobSalary.setText(entity.getSalary());
            jobItemHolder.CompanyName.setText(entity.getCompany_name());

        }

        @Override
        public int getItemCount() {
            return mDataList.size();
        }



        @Override
        public long getItemId(int position) {
            return mDataList.get(position).getId();
        }


        public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
            TextView JobName;
            TextView JobSalary;
            TextView CompanyName;
            View rootView;
            public int position;
            public ViewHolder(final View itemView) {
                super(itemView);
                JobName = (TextView) itemView.findViewById(R.id.searched_job_name);
                JobSalary = (TextView) itemView.findViewById(R.id.searched_job_salary);
                CompanyName = (TextView) itemView.findViewById(R.id.searched_company_name);
                rootView=itemView.findViewById(R.id.job_item_container);
                rootView.setOnClickListener(this);

            }
            @Override
              public void onClick(View v) {
                Intent intent=new Intent(context,details_job_Activity.class);
                intent.putExtra("JobId",mDataList.get(position).getId());
                startActivity(intent);
                  }
        }


    }


    public class  myJobListener  implements  OnResponseListener <Bean<Job>> {
        @Override
        public void onSuccess(Bean<Job> result) {
            Toast.makeText(context,"it succeded but "+String.valueOf(result.getTotal()),Toast.LENGTH_LONG).show();
            NewsListAdapter newsListAdapter =new NewsListAdapter(context,result.getList());
            recyclerView.setAdapter(newsListAdapter);
        }

        @Override
        public void onFailed(ErrorModel errorModel) {

        }
    }
}